//
//  WKFNetwork.m
//  质检
//
//  Created by Mac on 2018/3/28.
//  Copyright © 2018年 158682312@qq.com. All rights reserved.
//

#import "WKFNetwork.h"
#import "AFNetworking.h"
#import "DataCheck.h"
#import "JSONFunction.h"
#import "JsonReciprocity.h"

@implementation WKFNetwork
#pragma mark - init

//单例对象
+ (WKFNetwork *)sharedSingleton
{
    static dispatch_once_t pred = 0;
    __strong static id _sharedObject = nil;
    dispatch_once(&pred, ^{
        _sharedObject = [[self alloc] init];
    });
    return _sharedObject;
}


#pragma mark - Network begin
//开始网络请求
- (void)afNetworkBeginWithHost:(NSString *)aHostName path:(NSString *)aPath params:(NSDictionary *)aParams httpMethod:(NSString *)aHttpMethod selectorName:(NSString *)aSelectorName delegate:(__weak id)delegate
{
    NSMutableDictionary *addParams = [[NSMutableDictionary alloc] initWithDictionary:aParams];
    
    NSString *postUrl                           = [NSString stringWithFormat:@"%@%@",aHostName,aPath];  //URL
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer                  = [AFJSONResponseSerializer serializer];        //使用这个将得到的是JSON
    manager.requestSerializer                   = [AFJSONRequestSerializer serializer];
    manager.requestSerializer.timeoutInterval   = uTimeOut;                                     //TimeOut 超时
    manager.requestSerializer.stringEncoding    = NSUTF8StringEncoding;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
    //ContentTypes
    //[manager.operationQueue cancelAllOperations];
    
    [manager POST:postUrl parameters:addParams progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
     {
         /**
          Debug模式 打印出 get请求参数
          */
#ifdef DEBUG
         NSMutableString *paramsString = [NSMutableString string];
         NSArray *allkeys = [addParams allKeys];
         for (int i = 0 ; i<allkeys.count; i++)
         {
             NSString *key = [allkeys objectAtIndex:i];
             
             id value = [addParams objectForKey:key];
             //DLog(@"key is :%@,value is :%@",key,value);
             if (i==0) {
                 [paramsString appendFormat:@"%@=%@",key,value];
             }else{
                 [paramsString appendFormat:@"&%@=%@",key,value];
             }
         }
         
         DLog(@"\n /* ---------------------------------------------------------- \
              \n -- request Selector Name: %@ \n -- request URL:%@%@ \n -- request GET Params: %@ \n -- request Params: %@ \n -- request Success:\n -- %@ \n ",
              aSelectorName,aHostName,aPath,paramsString,[addParams JSONString],[responseObject JSONString]);
#endif
         
         
//         if (![DataCheck isValidDictionary:responseObject])
//         {
//             return;
//         }
         //解析数据
         
         ZXTAPIStatus *status = [[ZXTAPIStatus alloc] initWithDictionary:responseObject];
         
         BOOL networkSuccess = [[NSString stringWithFormat:@"%@",status.errorcode] isEqualToString:@"1"] ? YES : NO;
         //针对这个项目修改的请求，

         //返回不带请求参数的 成功数据
         [delegate networkSuccessWithResponseDic:responseObject networkPath:aPath statusModel:status paramsStr:aSelectorName];
//         if (networkSuccess)
//         {
//             /**
//              API成功 回调网络请求 回调
//              */
//             if (delegate && [delegate respondsToSelector:@selector(networkSuccessWithRequestDic:responseDic:networkPath:statusModel:paramsStr:)])
//             {
//                 //返回带请求参数的 成功数据
//                 [delegate networkSuccessWithRequestDic:aParams responseDic:responseObject networkPath:aPath statusModel:status paramsStr:aSelectorName];
//             }
//             else if (delegate && [delegate respondsToSelector:@selector(networkSuccessWithResponseDic:networkPath:statusModel:paramsStr:)])
//             {
//                 //返回不带请求参数的 成功数据
//                 [delegate networkSuccessWithResponseDic:responseObject networkPath:aPath statusModel:status paramsStr:aSelectorName];
//             }
//         }
//         else
//         {
//             /**
//              API失败 回调网络请求 回调
//              */
//             if (delegate && [delegate respondsToSelector:@selector(networkAPIError:networkPath:statusModel:)])
//             {
//                 [delegate networkAPIError:responseObject networkPath:aPath statusModel:status];
//             }
//         }
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
     {
         /**
          Debug模式 打印出 get请求参数
          */
         //         DLog(@"%@",operation. responseString);
         
#ifdef DEBUG
         NSMutableString *paramsString = [NSMutableString string];
         NSArray *allkeys = [addParams allKeys];
         for (int i = 0 ; i<allkeys.count; i++)
         {
             NSString *key = [allkeys objectAtIndex:i];
             
             id value = [addParams objectForKey:key];
             //DLog(@"key is :%@,value is :%@",key,value);
             if (i==0) {
                 [paramsString appendFormat:@"%@=%@",key,value];
             }else{
                 [paramsString appendFormat:@"&%@=%@",key,value];
             }
         }
         
         DLog(@"\n /* ------------------------------------------------------------------------------------------\
              \n -- request Selector Name: %@ \n -- request URL:%@%@ \n -- request Params: %@ \n -- request GET Params: %@  \n -- request Error:\n -- %@ \n ",aSelectorName,aHostName,aPath,[addParams toJsonString],paramsString,error)
#endif
         
         
         /**
          网络请求出错 回调
          */
         if (delegate && [delegate respondsToSelector:@selector(networkError:networkPath:)])
         {
             [delegate networkError:error networkPath:aPath];
         }
     }];
}
@end
